// pages/region/regionList/index.js
var basePage = require('../../../common/BasePage')
Page({
    behaviors: [basePage],
    /**
     * 页面的初始数据
     */
    data: {
        index: 1,
        provinceVal: {},
        province: [],
        provinceList: [],
        cityVal: {},
        citys: [],
        indexList: [],
        lat: 0,
        lng: 0,
        area_code: 0,
        district: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log("options", options)
        this.setData({
            lat: options.lat,
            lng: options.lng,
            area_code: options.area_code,
            district: options.district
        })
        this.getList()
    },
    //获取省
    getprovinceList() {
        this.request({
            url: this.data.host + "/home/addressSelect",
            data: {
                // lat: this.data.lat,
                // lng: this.data.lng,
                level: 0,
                area_code: 0
            }
        }, res => {
            this.setData({
                provinceList: res.data.letter,
                province: res.data.city_data
            })
        })
    },
    onSelectProvince(e) {
        var obj = e.currentTarget.dataset.obj
        this.setData({
            provinceVal: obj
        })
        this.getList(obj.id)
    },
    //获取市级
    getList(id) {
        this.request({
            url: this.data.host + "/home/addressSelect",
            data: {
                // lat: this.data.lat,
                // lng: this.data.lng,
                area_code: 0,
                level: 1
            }
        }, res => {
            this.setData({
                index: 1,
                indexList: res.data.letter,
                citys: res.data.city_data
            })

        })
    },
    changeIndex(e) {
        var index = e.currentTarget.dataset.index
        this.setData({
            index: index
        })
    },
    onSelectCity(e) {
        var obj = e.currentTarget.dataset.obj
        this.setData({
            cityVal: obj
        })
        this.getRegion(obj.id)
    },
    //获取区
    getRegion(code) {
        this.request({
            url: this.data.host + "/home/addressSelect",
            data: {
                // lat: this.data.lat,
                // lng: this.data.lng,
                area_code: code,
                level: 2
            }
        }, res => {
            if (res.data.city_data.length <= 0) {
                wx.showModal({
                  title: '提示',
                  content: '当前没有区级仓，正在招募城市仓，请到'+this.data.cityVal.fullname+"下单",
                  showCancel:false,
                  complete: (res) => {
                    if (res.cancel) {
                      
                    }
                
                    if (res.confirm) {
                        var data = {
                            // province_code:this.data.provinceVal.id,
                            // province_name:this.data.provinceVal.fullname,
                            // city_code:this.data.cityVal.id,
                            // city_name:this.data.cityVal.fullname,
                            address_code: this.data.cityVal.id,
                            level: 1
                        }
                        const eventChannel = this.getOpenerEventChannel()
                        eventChannel.emit('getRegion', {
                            data: data
                        });
                        wx.navigateBack({
                            delta: 1
                        })
                    }
                  }
                })
                
            } else {
                this.setData({
                    index: 2,
                    indexList2: res.data.letter,
                    regionList: res.data.city_data
                })
            }

        })
    },
    onSelectRegion(e) {
        var obj = e.currentTarget.dataset.obj
        var data = {
            // province_code:this.data.provinceVal.id,
            // province_name:this.data.provinceVal.fullname,
            // city_code:this.data.cityVal.id,
            // city_name:this.data.cityVal.fullname,
            // district_code:obj.id,
            // district_name:obj.fullname

            address_code: obj.id,
            level: 2
        }
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('getRegion', {
            data: data
        });
        wx.navigateBack({
            delta: 1
        })
    },
    onLocation() {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('getLocation', {
            data: 1
        });
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})